package com.rt.tmpt.controller.springmvc.admin;


import com.google.gson.Gson;
import com.rt.code.utils.web.WebUtils;
import com.rt.tmpt.dto.A2PostDto;
import com.rt.tmpt.dto.A2UserDto;
import com.rt.tmpt.dto.ConstantDto;
import com.rt.tmpt.dto.Result;
import com.rt.tmpt.service.IOperationlogService;
import com.rt.tmpt.service.IOrgService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;


@ApiIgnore
@Controller
@RequestMapping("/admin123/user")
@Slf4j
public class OrgAction{

    private static final long serialVersionUID = 1L;

	@Resource private IOrgService orgService;
	@Resource private IOperationlogService operationlogService;

	@RequestMapping(value = "/orgAjax_queryPostJsonTreeByCompanyId", produces = "application/json; charset=utf-8")
	@ResponseBody
	public String queryPostJsonTreeByCompanyId(HttpServletRequest request){
		String r = "";
		try{
			Long companyId = WebUtils.getCompanyId(request);
			companyId = companyId==null?0:companyId.longValue();

			Result resultObj = orgService.queryPostJsonTreeByCompanyId(companyId);
			if(!resultObj.isSuccess()){
				r = "数据加载失败";
				return r;
			}

			List<Map<String,Object>> jsonObjectResult  = (List<Map<String,Object>>)resultObj.getMap().get("list");
			return new Gson().toJson(jsonObjectResult);
		}catch (Exception e) {
			r = "出错";
			e.printStackTrace();
		}
		return r;
	}


	@RequestMapping(value = "/orgAjax_removePostNode", produces = "application/json; charset=utf-8")
	@ResponseBody
	public Result removePostNode(String postId){
		Result resultObj = new Result();
		try{
			resultObj = orgService.removePostById(Long.parseLong(postId));
		}catch (Exception e) {
			resultObj.fault("出错:"+e.getLocalizedMessage());
			e.printStackTrace();
		}

		return resultObj;
	}

	@RequestMapping(value = "/orgAjax_addeditPostNode", produces = "application/json; charset=utf-8")
	@ResponseBody
	public Result addeditPostNode(String name, String optType, String id, String companyId,
                                  HttpServletRequest request){
		Result resultObj = new Result();
		try{
			HttpSession session = request.getSession();
			A2UserDto userDto = (A2UserDto)session.getAttribute(ConstantDto.MagSystemDtoSession);
			long userId = userDto.getId();

			A2PostDto postDto = new A2PostDto();
			postDto.setName(name.trim());
			if("add".equals(optType)){
				//添加
				long parantId = 0;//根节点
				if(!id.toLowerCase().contains("cid")){
					parantId = Long.parseLong(id);
				}
				resultObj = orgService.addPost(postDto,parantId,Long.parseLong(companyId));
			}else if("edit".equals(optType)){
				//修改
				postDto.setId(Long.parseLong(id));
				resultObj = orgService.modifyPostById(postDto);
			}else{
				resultObj.fault("失败：没有相应操作类型");
			}

		}catch (Exception e) {
			resultObj.fault("出错:"+e.getLocalizedMessage());
			e.printStackTrace();
		}

		return resultObj;
	}



}
